# Continous variables as primary endpoint

library(Mediana)

# Data Model ----

# Standard scenario
control.dist.par1 <- parameters(mean = 5.7, sd = 0.30)
test.dist.par1 <- parameters(mean = 5.6, sd = 0.25)

# Optimistic scenario
control.dist.par2 <- parameters(mean = 5.7, sd = 0.30)
test.dist.par2 <- parameters(mean = 5.5, sd = 0.25)


dmTrial <- DataModel() +
  OutcomeDist(outcome.dist = "NormalDist") +
  # SampleSize(c(10, 50, 100)) +
  Sample(id = "Control",
         outcome.par = parameters(control.dist.par1, control.dist.par2),
         sample.size = c(25, 50, 100)) +
  Sample(id = "Test",
         outcome.par = parameters(test.dist.par1, test.dist.par2),
         sample.size = c(50, 100, 200))

# Analysis model ----
amTrial <- AnalysisModel() +
  Test(id = "Control_vs_Test",
       samples = samples("Control", "Test"),
       method = "TTest", 
       par = parameters(larger = FALSE)) # Control < Test in value

amTrial <- amTrial +
  Statistic(id = "Mean desc",
            method = "MeanStat",
            samples = samples("Test"))

# Evaluation model ----
emTrial <- EvaluationModel() +
  Criterion(id = "Marginal power",
            method = "MarginalPower",
            tests = tests("Control_vs_Test"),
            labels = c("Control vs Test"),
            par = parameters(alpha = 0.05))

emTrial <- emTrial +
  Criterion(id = "Average_Mean",
            method = "MeanSumm",
            statistics = statistics("Mean description"),
            labels = c("Average mean test"))

# Clinical Scenrio evaluation ----
sim_para <- SimParameters(n.sims = 10, proc.load = "full", seed = 1)
cseTrial <- CSE(dmTrial, amTrial, emTrial, sim_para)

summTrial <- summary(cseTrial)
summTrial

# Presentation ----
# pmTrial <- PresentationModel() +
#   Project(username = "Continous variable for normal distribution",
#           title = "Simulation trial",
#           description = "Clinical trial simulation") +
#   Section(by = "outcome.parameter") +
#   Table(by = "sample.size") +
#   CustomLabel(param = "sample.size",
#               label = paste0("N=", c(100, 120, 140)))
# 
# GenerateReport(presentation.model = pmTrial,
#                cse.results = cseTrial,
#                report.filename = "simx2.docx")
